package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"zg3/internal/api/request"
	"zg3/internal/api/response"
	"zg3/internal/models/service"
)

func SendFriendRequest(c *gin.Context) {
	var json request.SendFriendRequest
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))
	check := service.Check(&json, userId)

	if check != "" {
		response.Response(c, 400, check, nil)
		return
	}

	service.SendFriendRequest(&json, userId)

	response.Response(c, 200, "好友申请发送成功", nil)

}

func Yes(c *gin.Context) {
	var json request.YesOrNo
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	yes := service.Yes(&json, userId)

	if yes != "" {
		response.Response(c, 400, yes, nil)
		return
	}
	response.Response(c, 200, "好友添加成功", nil)
}

func No(c *gin.Context) {
	var json request.YesOrNo
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	service.No(&json, userId)

	response.Response(c, 200, "拒绝成功", nil)

}

func Top(c *gin.Context) {
	var json request.Top
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	service.Top(&json, userId)

	response.Response(c, 200, "置顶成功", nil)

}

func NoTop(c *gin.Context) {
	var json request.Top
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	service.NoTop(&json, userId)

	response.Response(c, 200, "取消置顶成功", nil)

}

func List(c *gin.Context) {
	var json request.FriendList
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	list := service.FriendList(&json, userId)

	response.Response(c, 200, "展示成功", list)

}

func Delete(c *gin.Context) {
	var json request.Delete
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	value, _ := c.Get("userId")
	userId, _ := strconv.Atoi(value.(string))

	s := service.Delete(&json, userId)

	if s != "" {
		response.Response(c, 400, s, nil)
	}

	response.Response(c, 200, "删除成功", nil)

}
